﻿using Furion;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gear.NoSql.Core.Mongo
{
    /// <summary>
    /// Mongo实例化
    /// </summary>
    
    public static class MongoClientSetting
    {
        public static string Client { get; set; }//连接串 
        public static string DataBase { get; set; }//GridFS库 一般用于存放文件
        public static string Collection { get; set; }//Collection库 一般用于存放文本

        public static string Bucket { get; set; }//Bucket
        public static int multiple { get; set; }//图像压缩倍数
        public static int threshold { get; set; }//图像压缩阈值
        static MongoClientSetting()
        {
            Client = App.Configuration["Mongo:Client"];
            DataBase = App.Configuration["Mongo:DataBase"];
            Collection = App.Configuration["Mongo:Collection"];
            Bucket = App.Configuration["Mongo:Bucket"];
            multiple = Convert.ToInt32(App.Configuration["Mongo:multiple"]);
            threshold = Convert.ToInt32(App.Configuration["Mongo:threshold"]);
        }
        /// <summary>
        /// 获取链接实例
        /// </summary>
        /// <returns></returns>
        public static IMongoDatabase GetConnection()
        {
            //连接数据库
            //connstr:"mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
            var client = new MongoClient(Client);
            //获取database
            return client.GetDatabase(DataBase);
        }
        /// <summary>
        /// 获取GridFS实例
        /// </summary>
        /// <returns></returns>
        public static GridFSBucket bucket(GridFSBucketOptions BucketOptions) => new GridFSBucket(GetConnection(), BucketOptions);
        /// <summary>
        /// 获取Collection实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collectionName"></param>
        /// <returns></returns>
        public static IMongoCollection<T> collection<T>(string collectionName) => GetConnection().GetCollection<T>(collectionName);
    }
}
